package scau.gsmg.misp.action;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.sf.jasperreports.engine.JasperCompileManager;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import scau.gsmg.misp.service.IStatisticsService;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionSupport;
/**
 * @Title 销售统计
 * @Group gaoshenmegui
 * @author ZCL
 * @date 2015年6月3日
 */
@Controller
@Scope("prototype")
@Namespace(value = "/")
@ParentPackage("json-default")
@Results({
	@Result(name = "json", type = "json", params = { "root", "jsonArray" }),
	@Result(name = "error", location = "/error.jsp") })
public class StatisticsAction  extends ActionSupport {
	
	private static final long serialVersionUID = 3006807123882804948L;
	private IStatisticsService statisticsService;
	private List list;
	private int year;
	private JSONArray jsonArray;
	private Date beginDate;
	private Date endDate;
	private static final Logger logger = Logger.getLogger(StatisticsAction.class);
	
	/**
	 * @Description 月销售报表
	 */
	public String monthReport() throws Exception {
		try {
			list = statisticsService.getMonthStatistics(year);
			String reportSource = ServletActionContext.getServletContext().getRealPath("/reports/report8.jrxml");
		    File parent = new File(reportSource).getParentFile();
		    JasperCompileManager.compileReportToFile(reportSource, new File(parent, "report8.jasper").getAbsolutePath());
			
//			String reportSource=ServletActionContext.getServletContext().getRealPath("/reports/report8.jasper");
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return "success";
	}
	
	public String monthExcelReport() throws Exception {
		try {
			list = statisticsService.getMonthStatistics(year);
			
			String reportSource = ServletActionContext.getServletContext().getRealPath("/reports/report8.jrxml");
		    File parent = new File(reportSource).getParentFile();
		    JasperCompileManager.compileReportToFile(reportSource, new File(parent, "report8.jasper").getAbsolutePath());
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return "success";
	}
	
	/**
	 * @Description 按商品类别统计报表
	 */
	public String saleReport() throws Exception {
		try {
			list = statisticsService.getSaleStatistics(beginDate ,endDate);
			String reportSource = ServletActionContext.getServletContext().getRealPath("/reports/report9.jrxml");
		    File parent = new File(reportSource).getParentFile();
		    JasperCompileManager.compileReportToFile(reportSource, new File(parent, "report9.jasper").getAbsolutePath());
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return "success";
	}
	
	/**
	 * @Description 月销售量图形报表
	 */
	@Action(value = "highcharts")
	public String highcharts() throws Exception {
		jsonArray = new JSONArray();
		JSONObject jsonObj = new JSONObject();
		try {
			List dataList = statisticsService.getChartsData(year);
			List xAxis = statisticsService.xAxis(year);
			jsonObj.put("name", "销售额");
			jsonObj.put("data",dataList);
			jsonObj.put("category",xAxis);
			jsonObj.put("year",year);
			jsonArray.add(jsonObj);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return "json";
	}
	
	
	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public IStatisticsService getStatisticsService() {
		return statisticsService;
	}
	@Resource
	public void setStatisticsService(IStatisticsService statisticsService) {
		this.statisticsService = statisticsService;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public JSONArray getJsonArray() {
		return jsonArray;
	}

	public void setJsonArray(JSONArray jsonArray) {
		this.jsonArray = jsonArray;
	}

	public Date getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
}
